<template>
	<view class="content">
		<view class="form">
			<view class="cu-form-group">
				<view class="title">用户名</view>
				<input placeholder="请输入用户名" v-model="account"></input>
			</view>
			<view class="cu-form-group border">
				<view class="title">密码</view>
				<input type="password" placeholder="请输入密码" v-model="password"></input>
			</view>
			<button class="cu-btn btn-default btn" @tap="bindLogin()">登录</button>
		</view>
	</view>
</template>

<script>
	
	import md5 from 'js-md5';
	export default {
		data() {
			return {
				//screeHeight: (window.screen.height - 44) + 'px',
				account: '',
				password: '',
			};
		},
		methods: {
			bindLogin() {
				// console.log(md5(this.password))
				if(this.account == '') {
					uni.showToast({
						duration: 2000,
						title: '请输入用户名',
						icon: 'none'
					})
					return
				}
				if(this.password == '') {
					uni.showToast({
						duration: 2000,
						title: '请输入密码',
						icon: 'none'
					})
					return
				}
				uni.showLoading({
					title: '请稍后'
				})
				uni.request({
					url: getApp().globalData.url + '/blade-auth/oauth/token',
				    //url: '/api/blade-auth/oauth/token',
					header: {
						'content-type': 'application/x-www-form-urlencoded',
						'Authorization': "Basic c3dvcmQ6c3dvcmRfc2VjcmV0", 
						//'Authorization':"YXBwOmFwcF9zZWNyZXQ=",
						'Tenant-Id':"000000"
					},
					method: "post",
					data: {
						grant_type: 'password',
						scope: 'all',
						//username: 'admin',
						//password: '21232f297a57a5a743894a0e4a801fc3',
						username: this.account,
						password: md5(this.password),
						tenantid: '000000'
					},
					success: (res) => {
						let data = res.data;
						console.log(data);
						if(data.access_token && data.access_token != '') {
							let data = res.data;
							getApp().globalData.token = data.access_token
							this.getUserInfo();
						} else if (data.error_code && data.error_code == 400) {
							uni.hideLoading();
							uni.showToast({
								duration: 2000,
								title: '用户名或密码不正确',
								icon: 'none'
							})
						} else {
							uni.hideLoading();
							uni.showToast({
								duration: 2000,
								title: '登录失败',
								icon: 'none'
							})
						}
					}
				});
			},
			
			// 获取用户信息
			getUserInfo() {
				let token = getApp().globalData.token;
				uni.request({
					url: getApp().globalData.url + '/blade-user/info',
					header: {
						'blade-auth': "bearer " + token
					},
					method: "get",
					success: (res) => {
						let code = res.data.code;
						if(code == 200) {
							let data = res.data.data;
							getApp().globalData.userData = data;
						}
						uni.hideLoading();
						uni.switchTab({
						    url: '/pages/index/index'
						});
					}
				});
			},
			
		}
	}
</script>

<style scoped>
	/*.content {
		width: 100%;
		background:#FFFFFF; //<!--url(../../static/imgs/bg1.png)-->
	}*/
	.form {
		box-sizing: border-box;	
		width: calc(100% - 80rpx);
		padding: 40rpx;
		margin: 120rpx 40rpx 0;
		border-radius: 16rpx;
		background: #FFFFFF;
	}
	
	.border {
		border-bottom: 1rpx solid #eee;
	}
	
	.btn {
		margin: 80rpx 0 60rpx;
	}
</style>
